package a66weding.com.jiehuntong.base;

import android.app.Activity;
import android.os.Message;
import android.view.KeyEvent;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.application.GlobalApplication;
import a66weding.com.jiehuntong.common.CommonHandler;
import a66weding.com.jiehuntong.common.utils.CUtils;

/**
 * @version :
 * @creator : AxDu
 * @create-time : 2016/5/27
 * @description :
 */
public class CommonBaseActivity extends Activity {

    public boolean  mExitFlag;


    private MyHandler mHandler;


    private static final int WHAT_EXIT_FLAG = 0x128;
    private static final int WHAT_TONEXT = 0x130;




    /**
     * 再按一次退出
     */
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (KeyEvent.KEYCODE_BACK == keyCode) {
            if (!mExitFlag) {
                CUtils.showToast(R.string.dblclick_to_exit);
                mExitFlag = true;
                mHandler.sendEmptyMessageDelayed(WHAT_EXIT_FLAG, 2000);
                return true;
            }else if(mExitFlag){
                ((GlobalApplication)getApplication()).getActivityManager().exitApp();
            }
        }
        return super.onKeyUp(keyCode, event);
    }



    /**
     * 自定义 Handler, 用于实现按两次退出
     */
    class MyHandler extends CommonHandler<Activity> {
        public MyHandler(Activity activity) {
            super(activity);
        }

        @Override
        protected void handle(Activity activity, Message msg) {
            switch (msg.what) {
                case WHAT_EXIT_FLAG:
                    if (null != activity) {

                    }
                    break;

            }
        }
    }


}
